﻿/**********************************************************************************************************
 * Copyright (c) 2012 Neio Zhou
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************************************************/
using Communication;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Zap;

namespace Communication.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for MessageReaderTest and is intended
    ///to contain all MessageReaderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MessageReaderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


       
        [TestMethod()]
        public void SendMessageTest()
        {
            var expected = new SendMessage();
            expected.Method = "M1";
            expected.ObjectName = "O1";
            expected.Parameters = new System.Collections.Generic.Dictionary<string, string>();
            expected.Parameters.Add("P1", "A''");
            expected.Parameters.Add("P2", "B|,\"OK\"");
            var msgString = expected.ToString();

            MessageReader target = new MessageReader();
            SendMessage actual;
            actual = target.Read(msgString) as SendMessage;
            Assert.AreEqual(expected.Token, actual.Token);
            Assert.AreEqual(expected.ObjectName, actual.ObjectName);
            Assert.AreEqual(expected.Method, actual.Method);
            Assert.AreEqual(expected.Parameters.Count, actual.Parameters.Count);
            Assert.AreEqual(expected.Parameters["P1"], actual.Parameters["P1"]);
            Assert.AreEqual(expected.Parameters["P2"], actual.Parameters["P2"]);
            
        }

        [TestMethod()]
        public void CallbackMessageTest()
        {
            var expected = new CallbackMessage("TT");
            expected.ReturnValue = "M1";
            expected.ReturnType = typeof(string);
            var msgString = expected.ToString();

            MessageReader target = new MessageReader();
            CallbackMessage actual;
            actual = target.Read(msgString) as CallbackMessage;
            Assert.AreEqual(expected.Token, actual.Token);
            Assert.AreEqual(expected.ReturnValue, actual.ReturnValue);

        }

        [TestMethod()]
        public void ExceptionMessageTest()
        {
            var expected = new ExceptionMessage("TT");
            expected.Message = "M1";
            var msgString = expected.ToString();

            MessageReader target = new MessageReader();
            ExceptionMessage actual;
            actual = target.Read(msgString) as ExceptionMessage;
            Assert.AreEqual(expected.Token, actual.Token);
            Assert.AreEqual(expected.Message, actual.Message);

        }

        [TestMethod()]
        public void EventRegistrationMessageTest()
        {
            var expected = new EventRegistrationMessage("TT");
            expected.ObjectName = "M1";
            expected.EventName = "OnClick";
            expected.IsRegister = true;
            var msgString = expected.ToString();

            MessageReader target = new MessageReader();
            EventRegistrationMessage actual;
            actual = target.Read(msgString) as EventRegistrationMessage;
            Assert.AreEqual(expected.Token, actual.Token);
            Assert.AreEqual(expected.IsRegister, actual.IsRegister);
            Assert.AreEqual(expected.ObjectName, actual.ObjectName);
            Assert.AreEqual(expected.EventName, actual.EventName);


        }

        [TestMethod()]
        public void EventMessageTest()
        {
            var expected = new EventMessage("TTTID");
            expected.EventName = "M1";
            
            expected.Parameters = new System.Collections.Generic.Dictionary<string, string>();
            expected.Parameters.Add("P1", "A''");
            expected.Parameters.Add("P2", "B\"OK\"");
            var msgString = expected.ToString();

            MessageReader target = new MessageReader();
            EventMessage actual;
            actual = target.Read(msgString) as EventMessage;
            Assert.AreEqual(expected.Token, actual.Token);
            Assert.AreEqual(expected.EventName, actual.EventName);
            Assert.AreEqual(expected.Parameters.Count, actual.Parameters.Count);
            Assert.AreEqual(expected.Parameters["P1"], actual.Parameters["P1"]);
            Assert.AreEqual(expected.Parameters["P2"], actual.Parameters["P2"]);

        }
    }
}
